package com.tansun.easycare.modules.sys.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.tansun.easycare.modules.sys.entity.XssSqlHttpServletRequestWrapper;

public class SessionFilter implements Filter {
	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        if(request  == null){
            chain.doFilter(request, response);
            return ;
        }
        String url = httpRequest.getParameter("url"); 
        Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(?:com\\.cn|net\\.cn|org\\.cn|com|net|org|cn|biz|info|cc|tv)",Pattern.CASE_INSENSITIVE); 
        if(!StringUtils.isEmpty(url) && p.matcher(url).find()){
        	return;
        }        		
        String requestPath  = httpRequest.getServletPath().split("\\?")[0];
        String reg = ".*?(save|delete|delect).*?";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(requestPath);
        
        String reg2 = ".*?(ruleProj|ruleproj).*?";
        Pattern pattern2 = Pattern.compile(reg);
        Matcher matcher2 = pattern.matcher(requestPath);
        
         if(matcher.matches() && matcher2.matches()){
             //扩展request，防止字符注入等
             XssSqlHttpServletRequestWrapper xssRequest = new XssSqlHttpServletRequestWrapper((HttpServletRequest)request);
             //获取URL的参数 get请求
             Map<Object, Object> urlParam=null ; //= xssRequest.getParameterMap();
             //获取post参数
             String param = xssRequest.getRequestParams();
             String uri = xssRequest.getRequestURI();
             String type = "3";
             if(uri.indexOf("delete")>-1 || uri.indexOf("delect")>-1) {
            	 type="4";
             }
             LogUtils.saveLog((HttpServletRequest) request, getTitle(uri),type, JSON.toJSONString(urlParam), param);
             //继续请求处理类
             chain.doFilter(xssRequest, response);
         }else{
             chain.doFilter(request, response);
         }
    }

	private Map<String, String> RuleModelMap;
	public String getTitle(String uri) {
		if(RuleModelMap==null){
			RuleModelMap = new HashMap<String, String>();
			RuleModelMap.put("dataModel", "数据模型");
			RuleModelMap.put("segmentation", "分段细分");
			RuleModelMap.put("conditionalSub", "条件细分");
			RuleModelMap.put("decisionTree", "决策树");
			RuleModelMap.put("policyTree", "策略树");
			RuleModelMap.put("decisionTable", "决策表");
			RuleModelMap.put("evaluationIndex", "赋值索引");
			RuleModelMap.put("singleTblIndex", "决策表");
			RuleModelMap.put("scoreModel", "评分模型");
			RuleModelMap.put("generalRule", "普通规则");
			RuleModelMap.put("ruleGroup", "规则集");
			RuleModelMap.put("policyRuleGroup", "规则集");
			RuleModelMap.put("ruleFlow", "决策流");
			RuleModelMap.put("ruleProject", "规则项目");
		}
		for(String key:RuleModelMap.keySet()) {
			if(uri.indexOf(key)>-1) {
				return RuleModelMap.get(key);
			}
		}
		return "";
	}

	public void init(FilterConfig cfg) throws ServletException {
	}

}
